home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / chrome / content / addonskin.js < prev    next >
Encoding:
JavaScript  |  2007-02-24  |  37.3 KB  |  993 lines

  1. /*
  2.     Performancing for Firefox Skin and Addon code
  3. */
  4.  
  5. var gGlobalPerFormancingAddons = []; //For testing only
  6. var gPFFLastStyleSheetURL = ""; //Last style sheet url
  7.  
  8. var gPFFAddonList = []; //List of addons for PFF
  9.  
  10. var pffAddonSkin = new Object();
  11.  
  12. pffAddonSkin = {
  13.     LastStyleSheetURL: "",
  14.     AddonList: [],
  15.     loadedAddonList: false
  16.     //SkinList: [],
  17.     //loadedSkinList: false
  18. }
  19.  
  20. /**************************************************************
  21. ********************* Skin Functions **************************
  22. ***************************************************************/
  23.  
  24. pffAddonSkin.onSkinListClick = function(anElement, AEvent, itemNum){
  25.     if(AEvent.button == 0){ //if left-click
  26.         //var temp = document.getElementById("performancing-skin-list");
  27.         var elementIDName2    = "li-s-" + itemNum;
  28.         var temp3 = document.getElementById(elementIDName2);
  29.         
  30.         var theDesc = temp3.getAttribute('tooltiptext');
  31.         var descBox = document.getElementById("performancing-skin-desc");
  32.         descBox.value = theDesc;
  33.         
  34.         document.getElementById("performancing-skin-desc-label").hidden = false;
  35.         descBox.hidden = false;
  36.     }else{ //if other click
  37.         //openWindowIn( true, decodeURI(event.originalTarget.getAttribute('tooltiptext')) );
  38.     }
  39. }
  40.  
  41. pffAddonSkin.appendStyleSheetToEditor = function(aDoc, aSkinURI) {
  42.     try{
  43.       var theStylesheet = aDoc.createElementNS("http://www.w3.org/1999/xhtml", "link");
  44.       theStylesheet.setAttribute("rel", "stylesheet");
  45.       theStylesheet.setAttribute("type", "text/css");
  46.       theStylesheet.setAttribute("href", aSkinURI);
  47.       var theHeaders = aDoc.getElementsByTagName('head');
  48.       theHeaders[theHeaders.length -1].appendChild(theStylesheet);
  49.     }catch(e){}
  50. }
  51.  
  52. pffAddonSkin.appendStyleSheetToPFF = function(aDoc, aSkinURI, aLdDef) {
  53.     var numSS = document.styleSheets.length;
  54.     var foundMatch = false;
  55.     for(var i=0; i< numSS; i++){
  56.         var theHREF = document.styleSheets[i].href;
  57.         if(this.LastStyleSheetURL == theHREF){
  58.             document.styleSheets[i].disabled = true;
  59.         }
  60.         if(theHREF == aSkinURI){
  61.             document.styleSheets[i].disabled = false;
  62.             foundMatch = true;
  63.         }else if(theHREF == "chrome://performancing/skin/overlay.css"){
  64.             if(aLdDef == "false"){
  65.                 document.styleSheets[i].disabled = true;
  66.             }else{
  67.                 document.styleSheets[i].disabled = false;
  68.             }
  69.         }
  70.     }
  71.     if(!foundMatch){
  72.         var pi = aDoc.createProcessingInstruction("xml-stylesheet", 
  73.                 "href=\"" + aSkinURI + "\" type=\"text/css\"");
  74.         aDoc.insertBefore(pi, aDoc.lastChild);
  75.     }
  76.     
  77.     //alert("foundMatch: " + foundMatch + "\naSkinURI: " + aSkinURI + "\nthis.LastStyleSheetURL: " + this.LastStyleSheetURL);
  78.     
  79.     this.LastStyleSheetURL = aSkinURI;
  80.     
  81.     //document.styleSheets[2].disabled = true
  82. }
  83.  
  84. pffAddonSkin.setSkinCSS = function(aSkinURI, aLdDef) {
  85.     var theEditor = performancingUI.getRichEditorWindow();
  86.     var theEditorPreview = performancingUI.getPreviewWindow();
  87.     
  88.     pffAddonSkin.appendStyleSheetToPFF(document, aSkinURI, aLdDef);
  89.     pffAddonSkin.appendStyleSheetToEditor(theEditor.document, aSkinURI);
  90.     //pffAddonSkin.appendStyleSheetToEditor(theEditorPreview.document, aSkinURI);
  91.     
  92. }
  93.  
  94. //Load the last saved Skin
  95. pffAddonSkin.loadLastSkin = function() {
  96.     var theLastSkinURI = null;
  97.     var theLastSkinLoadDefault = null;
  98.     try{
  99.         theLastSkinURI = gPerformancingUtil.prefs.getCharPref("settings.lastselected.skinuri");
  100.         theLastSkinLoadDefault = gPerformancingUtil.prefs.getCharPref("settings.lastselected.lddef");
  101.     }catch(e){}
  102.     
  103.     if(theLastSkinURI != ""){
  104.         var theURI = this.getSkinURI(theLastSkinURI);
  105.         
  106.         if(theURI != null && theURI != "chrome://performancing/skin/overlay.css" && theURI != ""){
  107.             pffAddonSkin.setSkinCSS(theURI, theLastSkinLoadDefault);
  108.         }
  109.     }
  110. }
  111.  
  112. //Get a full Skin path from file name
  113. pffAddonSkin.getSkinURI = function(aSkinSRCLeaf) {
  114.     var theSkinSRC = "";
  115.     if( aSkinSRCLeaf == "_default_"){
  116.         theSkinSRC = "chrome://performancing/skin/overlay.css";
  117.     }else{
  118.         var file = PerFormancingDirIO.get("ProfD");
  119.             file.append("performancing");
  120.             file.append("skins");
  121.             file.append(aSkinSRCLeaf);
  122.             
  123.         theSkinSRC = file.path;
  124.         theSkinSRC = theSkinSRC.toString().replace(/\\/g,"/");
  125.         theSkinSRC = encodeURI(theSkinSRC);
  126.         theSkinSRC = "file:///" +theSkinSRC; //Works on Linux and Mac?
  127.     }
  128.     return theSkinSRC;
  129. }
  130.  
  131. //Loads the selected Skin file
  132. pffAddonSkin.enableSelectedSkin = function() {
  133.     var theList = document.getElementById("performancing-skin-list");
  134.     var aElement  = theList.selectedItem;
  135.     var theSkinSRCLeaf = aElement.getAttribute("src");
  136.     var theSkinLdDef = aElement.getAttribute("lddef");
  137.     var theSkinSRC = "";
  138.     theSkinSRC = this.getSkinURI(theSkinSRCLeaf);
  139.     //alert("theSkinSRC: " + theSkinSRC + " \n\ntheSkinSRCLeaf: " + theSkinSRCLeaf);
  140.         
  141.         
  142.     if(theSkinSRC == ""){
  143.         //alert("Couldn't find CSS file");
  144.     }else{
  145.         pffAddonSkin.setSkinCSS(theSkinSRC, theSkinLdDef);
  146.         //Set pref
  147.         gPerformancingUtil.prefs.setCharPref("settings.lastselected.skinuri", theSkinSRCLeaf);
  148.         gPerformancingUtil.prefs.setCharPref("settings.lastselected.lddef", theSkinLdDef);
  149.         
  150.         //Make sure the other elements are not enabled
  151.         var theParent = aElement.parentNode;
  152.         for(var i=0; i < theParent.childNodes.length; i++){
  153.             if(theParent.childNodes[i].localName == "listitem"){
  154.                 theParent.childNodes[i].setAttribute("enabled", false);
  155.             }
  156.         }
  157.         //Enable current selection
  158.         aElement.setAttribute("enabled", "true");
  159.         
  160.         //var errorStr = performancingUI.getLocaleString('skin.restartfx', []);//skin.restartfx
  161.         //alert(errorStr);
  162.     }
  163. }
  164.  
  165. //Add a Skin file
  166. pffAddonSkin.addASkinFile = function() {
  167.     //Load File Picker
  168.     var theFile = this.filePicker();
  169.     if(theFile.leafName.match(".css")){
  170.         this.handleTheFile( theFile );
  171.     }else if(theFile.leafName.match(".zip")){
  172.         this.handleSkinZip(theFile);
  173.     }
  174. }
  175.  
  176.  
  177. //Once we have an nsIFIle, play with it here.
  178. pffAddonSkin.handleTheFile = function(aFile) {
  179.     //Let's make sure it's a good file before we copy it.
  180.     var isGood = this.processSkinFile(aFile);
  181.     
  182.     if(isGood[0] == true){
  183.         
  184.         var file = PerFormancingDirIO.get("ProfD");
  185.         file.append("performancing");
  186.         file.append("skins");
  187.         //var myDir = file.clone();
  188.         file.append(aFile.leafName);
  189.         var alreadyExists = false;
  190.         if(file.exists()){
  191.             alreadyExists = true;
  192.         }
  193.         //Let's always overwrite it so user has the latest
  194.         var theoldFile = PerFormancingFileIO.open(aFile.path);
  195.         var theOldContent = PerFormancingFileIO.read(theoldFile, "UTF-8");
  196.         PerFormancingFileIO.create(file);
  197.         
  198.         var isSaved = this.saveContentsToFile( file, theOldContent );
  199.         
  200.         if(isSaved){
  201.             //Good, now add the file and save it
  202.             var filePath = file.leafName;
  203.             this.addSkinToXMLAndList( isGood, filePath );
  204.             
  205.         }
  206.     }
  207. }
  208.  
  209. pffAddonSkin.addSkinToXMLAndList = function(aSkinDesc, fileName) {
  210.     var theList =  document.getElementById("performancing-skin-list");
  211.     var theNumber = theList.getRowCount() + 1;
  212.     var wasOverwritten = this.saveXMLFile("skin", aSkinDesc[3], aSkinDesc[2], aSkinDesc[1], aSkinDesc[5], fileName, false );
  213.     if(wasOverwritten == null){
  214.         //Error writing to file
  215.     }else if(!wasOverwritten){
  216.         this.loadSkinAddonList("skin", "performancing-skin-list", theNumber, aSkinDesc[3], aSkinDesc[2], aSkinDesc[1], fileName, false, aSkinDesc[5] );
  217.     }
  218. }
  219.  
  220. pffAddonSkin.processSkinFile = function(aFile) {
  221.     
  222.     var theFileContent = PerFormancingFileIO.read(aFile, "UTF-8");
  223.     var theType     = "";
  224.     var theAuthor   = "";
  225.     var theName     = "";
  226.     var theDesc     = "";
  227.     var theDef      = "";
  228.     var thePFFVer   = "";
  229.     var noError     = true;
  230.     try{
  231.         theType    = /(?:@type:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  232.         theAuthor  = /(?:@author:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  233.         theName    = /(?:@name:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  234.         theDesc    = /(?:@desc:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  235.         theDef     = /(?:@lddef:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  236.         thePFFVer  = /(?:@pffver:[\s^\n^\f^\r]*)(.*)(?:[\n\f\r])/i.exec(theFileContent);
  237.         
  238.         theAuthor   = theAuthor[1];
  239.         theName     = theName[1];
  240.         theDesc     = theDesc[1];
  241.         theType     = theType[1];
  242.         theDef      = theDef[1];
  243.         thePFFVer   = thePFFVer[1];
  244.     }catch(e){
  245.         noError = false;
  246.         var errorStr = performancingUI.getLocaleString('validskinfilenotfound', []);//skin.restartfx
  247.         alert(errorStr + "\n" + e);
  248.     }
  249.     //alert("theType: " + theType + "\ntheAuthor: " + theAuthor + "\ntheName: " + theName + "\ntheDesc: " + theDesc +  "\ntheDefault: " + theDef );
  250.     theFileContent = null;
  251.     if(noError && theType == "PFF Skin"){
  252.         return [true, theAuthor, theName, theDesc, theType, theDef, thePFFVer ];
  253.     }else{
  254.         return [false]; //Bad file, so return false
  255.     }
  256.     return [false];
  257. }
  258.  
  259.  
  260. pffAddonSkin.deleteASkinFile = function() {
  261.     //Delete File from the UI list
  262.     var theList = document.getElementById("performancing-skin-list");
  263.     var theElement      = theList.selectedItem;
  264.     var theElemIndex    = theList.selectedIndex;
  265.     var theSkinSRCLeaf  = theElement.getAttribute("src");
  266.     if( theSkinSRCLeaf == "_default_"){
  267.         //Down't allow to delete!!!
  268.     }else{
  269.         //Delete from XML file
  270.         var didDelete = this.deleteItemFromXML(theSkinSRCLeaf);
  271.         //Delete the file
  272.         var didFDelete = this.deleteFile(theSkinSRCLeaf);
  273.         try{
  274.             didDirDelete = this.deleteDir(theSkinSRCLeaf);
  275.         }catch(e){}
  276.         //Delete from list
  277.         theList.removeItemAt(theElemIndex);
  278.     }
  279. }
  280.  
  281. // Handle Zip files that contain a valid PFF Skin
  282. // Conditions: Zip must contain the _skin_name_.css file in the parent dir
  283. // All files needed (images, etc.) should be in a directory called _skin_name_
  284. // The _skin_file_name_ dir can be multiple levels deep
  285. // Example Structure:
  286. //          myskin.zip
  287. //                    \myskin.css
  288. //                    \myskin\image.png
  289. //                    \myskin\*.*
  290. //
  291. pffAddonSkin.handleSkinZip = function(zipFile){
  292.     gPerformancingUtil.printLog("Start Skin Zip Functions!");
  293.     var zipReader = pffAddonSkin.getZipReaderForFile(zipFile);
  294.     zipReader.test(null);
  295.     try{
  296.         var entries = zipReader.findEntries("*.css");
  297.         var entry = entries.getNext().QueryInterface(Components.interfaces.nsIZipEntry);
  298.         //Extract to temp dir and process
  299.         var theCSSFileName = entry.name;
  300.         var file = PerFormancingDirIO.get("TmpD"); //Get's systems temp dir
  301.         file.append(entry.name);
  302.         
  303.         zipReader.extract(entry.name, file);
  304.         
  305.         //Now test the css
  306.         var isValidCSS = this.processSkinFile(file);
  307.         //What ever, delete the temp file, be nice to users temp folder ;)
  308.         PerFormancingFileIO.unlink(file);
  309.         
  310.         var isRightVersion = (gPerformancingVersion.match(/(\d\.\d)/)[0] - isValidCSS[6].match(/(\d\.\d)/)[0] >=0 );
  311.         //If it's valid, extract all files in
  312.         if(isValidCSS[0] == true && isRightVersion){
  313.             var theDir = pffAddonSkin.getSkinAddonDir(entry.name);
  314.             entries = zipReader.findEntries("*");
  315.             
  316.             while (entries.hasMoreElements()) {
  317.                 entry = entries.getNext().QueryInterface(Components.interfaces.nsIZipEntry);
  318.                 var parts = entry.name.split("/");
  319.                 var fileName = parts[parts.length-1];
  320.                 if (fileName != "") {
  321.                     var theFileToAdd = theDir.clone();
  322.                     for(var i=0; i< parts.length -1; i++){ //List all dirs
  323.                         theFileToAdd.append(parts[i]);
  324.                         PerFormancingDirIO.create(theFileToAdd); //create the dir
  325.                     }
  326.                     theFileToAdd.append(fileName); //Add the leaf to path
  327.                     zipReader.extract(entry.name, theFileToAdd); //Now extract the file
  328.                 }
  329.             }
  330.             this.addSkinToXMLAndList( isValidCSS, theCSSFileName );
  331.         }else{
  332.             var errorStr = performancingUI.getLocaleString('validskinfilenotfound', []);//skin.restartfx
  333.             alert(errorStr);
  334.         }
  335.     }catch(e){}
  336.     //Always close the reader
  337.     zipReader.close();
  338. }
  339.  
  340. /**************************************************************
  341. ********************* Addon Functions *************************
  342. ***************************************************************/
  343.  
  344. pffAddonSkin.onAddonListClick = function(event, itemNum){
  345.     if(event.button == 0){ //if left-click
  346.         //li-check-i-1
  347.         var temp = document.getElementById("performancing-addon-list");
  348.         //alert('Checked: ' + temp.selectedIndex );
  349.         //var elementIDName     = "li-check-i-" + (temp.selectedIndex + 1);
  350.         var elementIDName  = "li-a-check-i-" + itemNum;
  351.         var temp2 = document.getElementById(elementIDName);
  352.         temp2.checked = !temp2.checked;
  353.         //deleteTodoItem(itemNum);
  354.         
  355.         //var elementIDName2  = "li-" + (temp.selectedIndex + 1);
  356.         var elementIDName2    = "li-a-" + itemNum;
  357.         var temp3 = document.getElementById(elementIDName2);
  358.         if(temp2.checked){
  359.         //window.setTimeout( "hideElement('"+elementIDName2+"')" , 1500, true);
  360.             temp3.setAttribute('class', 'pffAddonBody');
  361.             temp3.setAttribute('enabled', 'true');
  362.         }else{
  363.             temp3.setAttribute('class', '');
  364.         }
  365.         
  366.         var elementIDName3    = "li-a-enabled-i-" + itemNum;
  367.         var temp4 = document.getElementById(elementIDName3);
  368.         var enabledStr = performancingUI.getLocaleString('enabled', []);
  369.         var disabledStr = performancingUI.getLocaleString('disabled', []);
  370.         var theSelectedElement = temp2.parentNode.parentNode; //li-a-#
  371.         var isEnabled = enabledStr;
  372.         if(temp2.checked){
  373.             isEnabled = enabledStr;
  374.             pffAddonSkin.enableSelectedAddon(theSelectedElement);
  375.         }else{
  376.             isEnabled = disabledStr;
  377.             pffAddonSkin.disableSelectedAddon(theSelectedElement);
  378.         }
  379.         temp4.setAttribute('label', isEnabled);
  380.         
  381.         var theDesc = temp3.getAttribute('tooltiptext');
  382.         var descBox = document.getElementById("performancing-addon-desc");
  383.         descBox.value = theDesc;
  384.         
  385.         document.getElementById("performancing-addon-desc-label").hidden = false;
  386.         descBox.hidden = false;
  387.         
  388.     }else{ //if other click
  389.         //openWindowIn( true, decodeURI(event.originalTarget.getAttribute('tooltiptext')) );
  390.     }
  391. }
  392.  
  393. //TODO
  394. pffAddonSkin.onAddonTabClick = function(aTab) {
  395.     var theTabName = aTab.getAttribute("name");
  396.     //theMainDeck.setAttribute('selectedIndex', parseInt(theTabIndex) + 6);
  397.     var theContentname = 'performancing-'+ theTabName +'-content';
  398.     var theDeck = document.getElementById("performancing-main-content-deck");
  399.     for(var i=0; i < theDeck.childNodes.length; i++){
  400.         if(theDeck.childNodes[i].id == theContentname){
  401.             //document.getElementById(theContentname).setAttribute('collapsed', false);
  402.             theDeck.setAttribute('selectedIndex',i);
  403.             gPerformancingUtil.sendAddonInitNotification("performancing-addon-pfftabclick-topic", theTabName);
  404.         }
  405.     }
  406. }
  407.  
  408. //TODO
  409. //Loads the selected Skin file
  410. pffAddonSkin.enableSelectedAddon = function(aElement) {
  411.     var theAddonName = aElement.getAttribute("src");
  412.     
  413.     //Get Pref File
  414.     var isInList = null;
  415.     var theAddonEnabledList = gPerformancingUtil.prefs.getCharPref("addons.enabledlist");
  416.     if(theAddonEnabledList != ""){
  417.         //Make sure it doesn't already exists
  418.         isInList = theAddonEnabledList.match(theAddonName);
  419.     }
  420.     if(isInList == null){
  421.         //Add to Pref list
  422.         theAddonEnabledList = theAddonEnabledList + theAddonName + ",";
  423.     }
  424.     
  425.     //Set pref
  426.     gPerformancingUtil.prefs.setCharPref("addons.enabledlist", theAddonEnabledList);
  427.     
  428.     //Tell addon to launch
  429.     gPerformancingUtil.sendAddonInitNotification("performancing-addon-pffenable-topic", theAddonName);
  430.     
  431. }
  432.  
  433. //TODO
  434. //Loads the selected Skin file
  435. pffAddonSkin.disableSelectedAddon = function(aElement) {
  436.     var theAddonName = aElement.getAttribute("src");
  437.     //Get Pref file
  438.     var theAddonEnabledList = gPerformancingUtil.prefs.getCharPref("addons.enabledlist");
  439.     
  440.     //Remove it from the list
  441.     theAddonEnabledList = theAddonEnabledList.replace(theAddonName+",", "");
  442.     //Save pref
  443.     gPerformancingUtil.prefs.setCharPref("addons.enabledlist", theAddonEnabledList);
  444.     gPerformancingUtil.sendAddonInitNotification("performancing-addon-pffdisable-topic", theAddonName);
  445. }
  446.  
  447. pffAddonSkin.checkListForDupe = function( aAddonName ){
  448.     var tempList = this.AddonList;
  449.     for(var i=0; i< tempList.length; i++ ){
  450.         if(tempList[i].id ==  aAddonName ){
  451.             return true;
  452.         }
  453.     }
  454.     return false;
  455. }
  456.  
  457. pffAddonSkin.hookInAddon = function( aAddonObject ){
  458.     var theAddonName = aAddonObject.id;
  459.     var theAddonVer  = aAddonObject.pffver;
  460.     
  461.     //Make sure the version is supported
  462.     //alert("gPerformancingVersion: " + gPerformancingVersion + " theAddonVer: " + theAddonVer);
  463.     var isSupportedVer = (gPerformancingVersion.match(/(\d\.\d)/)[0] - String(theAddonVer).match(/(\d\.\d)/)[0] >=0 );
  464.     if(isSupportedVer){
  465.         //Add to population list
  466.         //make sure it's not already added
  467.         var isAlreadyThere = this.checkListForDupe(theAddonName);
  468.         if(!isAlreadyThere){
  469.             this.AddonList.push( {name: aAddonObject.name, version: aAddonObject.version, description: aAddonObject.description, author: aAddonObject.author, id: aAddonObject.id, pffver: aAddonObject.pffver} );
  470.         }
  471.         
  472.         //Check if it's in the white list.
  473.         var isInList = null;
  474.         var theAddonEnabledList = gPerformancingUtil.prefs.getCharPref("addons.enabledlist");
  475.         if(theAddonEnabledList != ""){
  476.             //Make sure it doesn't already exists
  477.             isInList = theAddonEnabledList.match(theAddonName + ",");
  478.         }
  479.         
  480.         // if so, enable and return true, else false
  481.         //isInList = true;
  482.         if(isInList != null){
  483.             var theAddonTab = document.getElementById("performancing-addons-tab");
  484.             theAddonTab.hidden = false;
  485.             pffAddonSkin.doAddonEnabling(aAddonObject);
  486.         }
  487.     }else{
  488.         //alert("Wrong version of " + theAddonName);
  489.     }
  490. }
  491.  
  492. pffAddonSkin.doAddonEnabling = function( aAddonObject ){
  493.     var aAddonName = aAddonObject.id;
  494.     //Enable the Tab
  495.     document.getElementById('performancing-'+ aAddonName +'-tab').setAttribute('collapsed', false);
  496.     
  497.     aAddonObject.hookIntoPFF(true);
  498. }
  499.  
  500. pffAddonSkin.doAddonDisabling = function( aAddonObject ){
  501.     var aAddonName = aAddonObject.id;
  502.     //Enable the Tab
  503.     document.getElementById('performancing-'+ aAddonName +'-tab').setAttribute('collapsed', true);
  504.     
  505.     aAddonObject.hookIntoPFF(false);
  506. }
  507.  
  508. pffAddonSkin.enableAnAddonCallback = function( aAddonObject, aName ){
  509.     gPerformancingUtil.printLog("Enable aAddonObject.name: " + aAddonObject.name + "\naName: " + aName);
  510.     //Double check
  511.     if(aAddonObject.id == aName){
  512.         //Enable this addon
  513.         pffAddonSkin.hookInAddon(aAddonObject);
  514.     }
  515. }
  516.  
  517. pffAddonSkin.disableAnAddonCallback = function( aAddonObject, aName ){
  518.     gPerformancingUtil.printLog("Disable aAddonObject.name: " + aAddonObject.name + "\naName: " + aName);
  519.     //Double check
  520.     if(aAddonObject.id == aName){
  521.         //Enable this addon
  522.         pffAddonSkin.doAddonDisabling(aAddonObject);
  523.     }
  524. }
  525. //li-a-0
  526. pffAddonSkin.loadAddonList = function( ){
  527.     var theListArr = this.AddonList;
  528.     var theWhiteList = gPerformancingUtil.prefs.getCharPref("addons.enabledlist");
  529.     if(!this.loadedAddonList){
  530.         if(theListArr.length > 0){
  531.             gPerformancingUtil.clearListOut('performancing-addon-list');
  532.             for(var i=0; i < theListArr.length; i++){
  533.                 var enabled = false;
  534.                 if( theWhiteList.match(theListArr[i].id + ",") != null ){
  535.                     enabled = true;
  536.                 }//name version description author id
  537.                 this.loadSkinAddonList("addon", 'performancing-addon-list', 
  538.                                         i, theListArr[i].description, theListArr[i].name, theListArr[i].author, 
  539.                                         theListArr[i].id, enabled, "");
  540.             }
  541.             this.loadedAddonList = true;
  542.         }
  543.     }
  544. }
  545.  
  546. /**************************************************************
  547. *********** Common (skin & addon) Functions ******************* 
  548. ***************************************************************/
  549.  
  550.  
  551. pffAddonSkin.onTabSwitch = function(aTab) {
  552.     var theId = aTab.selectedItem.id;
  553.     if(theId == "performancing-tab-skin"){
  554.         pffAddonSkin.openAndLoadXMLFile('skin');
  555.     }else if(theId == "performancing-tab-addons"){
  556.         pffAddonSkin.loadAddonList();
  557.     }
  558. }
  559.  
  560. pffAddonSkin.loadAddonTabs = function(aTab) {
  561.     var theAddonBox = document.getElementById("performancing-navbar-tabs-addons");
  562.     var theToggle = Boolean(theAddonBox.hidden);
  563.     theAddonBox.hidden = !theToggle;
  564.     
  565.     var theAddonEnabledList = gPerformancingUtil.prefs.getCharPref("addons.enabledlist");
  566.     if(theAddonEnabledList == ""){
  567.         //Open the settings tab
  568.         performancingUI.onLeftSidebarTabSelect('settings');
  569.         var settingsTabs = document.getElementById("pff-addon-tabbox");
  570.         settingsTabs.selectedIndex = 2
  571.     }
  572. }
  573.  
  574.  
  575. //File Picker
  576. pffAddonSkin.filePicker = function() {
  577.     const nsIFilePicker = Components.interfaces.nsIFilePicker;
  578.     var fp = Components.classes["@mozilla.org/filepicker;1"]
  579.                .createInstance(nsIFilePicker);
  580.     fp.init(window, "Dialog Title", nsIFilePicker.modeOpen);
  581.     fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
  582.     
  583.     var rv = fp.show();
  584.     if (rv == nsIFilePicker.returnOK){
  585.       var file = fp.file;
  586.       //var fileName = file.leafName;
  587.       return file;
  588.     }else{
  589.         return null;
  590.     }
  591. }
  592.  
  593. // On tab select, if we havn't loaded it yet, load the list
  594. // aService = 'addon' | 'skin'
  595. pffAddonSkin.loadSkinAddonList = function(aService, listIDname, aNumber, aDesc, aName, aAuthor, aSRC, isEnabled, aLdDef) {
  596.     var list = document.getElementById(listIDname);
  597.     var item = document.createElement('listitem');
  598.     
  599.     item.setAttribute('tooltiptext', aDesc);
  600.     item.setAttribute('enabled', isEnabled);
  601.     item.setAttribute('class', "pffAddonBody");
  602.     item.setAttribute('src', aSRC);
  603.     item.setAttribute('lddef', aLdDef);
  604.     //item.setAttribute('name', aName);
  605.     //item.setAttribute('author', aAuthor);
  606.     
  607.     //Now done with an XBL binding
  608.     var itemCell = document.createElement('listcell');
  609.     
  610.     
  611.     //Same for both
  612.     var itemCell2 = document.createElement('listcell');
  613.     itemCell2.setAttribute('label', aAuthor);
  614.     itemCell2.setAttribute('crop', 'right');
  615.     itemCell2.setAttribute('class', "addonAuth");
  616.     
  617.     if(aService == "addon"){
  618.         item.setAttribute('id', 'li-a-' + aNumber);
  619.         item.setAttribute('onclick', "pffAddonSkin.onAddonListClick(event, '" + aNumber + "')");
  620.         
  621.         var itemCell3 = document.createElement('listcell');
  622.         
  623.         var checkbox = document.createElement('checkbox');
  624.         var label = document.createElement('label');
  625.         checkbox.setAttribute('id', 'li-a-check-i-' + aNumber);
  626.         checkbox.setAttribute('crop', 'false');
  627.         checkbox.setAttribute('class', 'pffAddonCheck');
  628.         checkbox.setAttribute('checked', isEnabled);
  629.         label.setAttribute('value', aName);
  630.         itemCell.appendChild(checkbox);
  631.         itemCell.appendChild(label);
  632.         itemCell3.setAttribute('id', 'li-a-enabled-i-' + aNumber);
  633.         itemCell3.setAttribute('crop', 'right');
  634.         var theStr = "";
  635.         if(isEnabled){
  636.             theStr = performancingUI.getLocaleString('enabled', []);
  637.         }else{
  638.             theStr = performancingUI.getLocaleString('disabled', []);
  639.         }
  640.         itemCell3.setAttribute('label', theStr);
  641.     }else{
  642.         itemCell.setAttribute('label', aName);
  643.         itemCell.setAttribute('crop', 'right');
  644.         itemCell.setAttribute('class', "addonName");
  645.         item.setAttribute('id', 'li-s-' + aNumber);
  646.         item.setAttribute('onclick', "pffAddonSkin.onSkinListClick(this, event, '" + aNumber + "')");
  647.     }
  648.     item.appendChild(itemCell);
  649.     item.appendChild(itemCell2);
  650.     if(aService == "addon"){
  651.         item.appendChild(itemCell3);
  652.     }
  653.     //Append the elements
  654.     list.appendChild(item);
  655. }
  656.  
  657. // aService = 'addon' | 'skin'
  658. pffAddonSkin.getXMLFile = function() {
  659.     var file = PerFormancingDirIO.get("ProfD");
  660.     file.append("performancing");
  661.     //file.append("skins");
  662.     file.append("pffaddons.xml");
  663.     return file;
  664. }
  665.  
  666. pffAddonSkin.deleteItemFromXML = function(aSRC) {
  667.     var file = this.getXMLFile();
  668.     if(file.exists()){
  669.         var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  670.         var theXML = new XML(theXMLString);
  671.         var indexToDelete = theXML..entry.(src==aSRC).childIndex();
  672.         var didDelete = delete theXML..entry[indexToDelete];
  673.         var isSaved = this.saveContentsToFile( file, theXML.toString() );
  674.         
  675.         return (didDelete && isSaved) ;
  676.     }else{
  677.         return false;
  678.     }
  679.     return false;
  680. }
  681.  
  682. pffAddonSkin.deleteFile = function(aSRC) {
  683.     var theFile = this.getSkinAddonFile(aSRC);
  684.     if( theFile.exists() ){
  685.         PerFormancingFileIO.unlink(theFile);
  686.     }else{
  687.         //alert("Couldn't delete file");
  688.     }
  689. }
  690.  
  691. pffAddonSkin.deleteDir = function(aSRC) {
  692.     var theFile = this.getSkinAddonDir(aSRC);
  693.     var dirName = aSRC.split(".");
  694.     theFile.append(dirName[0]);
  695.     if( theFile.exists() ){
  696.         if(theFile.isDirectory()){
  697.             PerFormancingDirIO.unlink(theFile, true); //true => recursive
  698.         }
  699.     }
  700. }
  701.  
  702. pffAddonSkin.openAndLoadXMLFile = function(aService) {
  703.     var file = this.getXMLFile();
  704.     if(file.exists()){
  705.         var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  706.         var theXML = new XML(theXMLString);
  707.         var allTheAddons = null;
  708.         if(aService=="addon"){
  709.             allTheAddons = theXML.addons;
  710.         }else{
  711.             allTheAddons = theXML.skins;
  712.         }
  713.         //gGlobalPerFormancingAddons = theXML;
  714.         
  715.         var theLastSkinURI = gPerformancingUtil.prefs.getCharPref("settings.lastselected.skinuri");
  716.         //var theLastSkinLdDef = gPerformancingUtil.prefs.getCharPref("settings.lastselected.lddef");
  717.         if(allTheAddons.children().length() > 0){
  718.             gPerformancingUtil.clearListOut('performancing-'+aService+'-list');
  719.             for(var j = 0; j < allTheAddons.children().length(); j++){
  720.                 var entryDesc = allTheAddons.entry[j].desc.toString();
  721.                 var entryName = allTheAddons.entry[j].name.toString();
  722.                 var entryAuthor = allTheAddons.entry[j].author.toString();
  723.                 //entryDate = allTheAddons.entry[j].date.toString();
  724.                 var entrySRC = allTheAddons.entry[j].src.toString();
  725.                 var setLdDef = allTheAddons.entry[j].defaulton.toString();
  726.                 //alert("entrySRC: " + entrySRC + "\ntheLastSkinURI: " + theLastSkinURI);
  727.                 var isEntryEnabled = "false";
  728.                 if(theLastSkinURI == entrySRC){
  729.                     isEntryEnabled = "true";
  730.                 }else{
  731.                     isEntryEnabled = "false";
  732.                 }
  733.                 this.loadSkinAddonList(aService, 'performancing-' + aService + '-list', j, entryDesc, entryName, entryAuthor, entrySRC, isEntryEnabled, setLdDef)
  734.             }
  735.         }
  736.     }else{
  737.         //alert("No XML file");//TEMP
  738.     }
  739.     
  740. }
  741.  
  742. // Here we save the XML data and create it if it doesn't exist
  743. // aService = 'addon' | 'skin'
  744. pffAddonSkin.saveXMLFile = function(aService, theDesc, theName, theAuthor, isDefaultOn, theSRC, isEnabled, aType ) {
  745.     var file = this.getXMLFile();
  746.     var theXMLFile = null;
  747.     var theAddonXML = null;
  748.     var theDateAdded = bfXMLRPC.iso8601Format( new Date() );
  749.     var didOverWrite = false;
  750.     var isSaved = false;
  751.     
  752.     //Doesn't exist, so let's create it.
  753.     if(!file.exists()){
  754.         //dump('Creating file: ' + bloglistxmlfile + "\n");
  755.         var defName = "Default";
  756.         var defDesc = "PFF's Default Theme";
  757.         var defAuthor = "Performancing, Inc.";
  758.         var defSRC = "_default_";
  759.         var defDate = "20060711T22:19:53";
  760.         var defEnable = "true";
  761.         theAddonXML = <addonlist>
  762.                         <skins>
  763.                             <entry>
  764.                               <name>{defName}</name>
  765.                               <desc>{defDesc}</desc>
  766.                               <author>{defAuthor}</author>
  767.                               <src>{defSRC}</src>
  768.                               <date>{defDate}</date>
  769.                               <enabled>{defEnable}</enabled>
  770.                               <defaulton>{isDefaultOn}</defaulton>
  771.                               <type>{aType}</type>
  772.                             </entry>
  773.                         </skins>
  774.                         <addons />
  775.                      </addonlist>;
  776.         PerFormancingFileIO.create(file);
  777.         
  778.         isSaved = this.saveContentsToFile( file, theAddonXML.toString() );
  779.     }
  780.     
  781.     if(file.exists()){
  782.         var theXMLFile = PerFormancingFileIO.open(file.path);
  783.         var theXMLString = PerFormancingFileIO.read(theXMLFile, "UTF-8");
  784.         var theAddonXML = new XML(theXMLString);
  785.         
  786.         //The Content for each Blog
  787.         if(aService == "skin"){
  788.             var itExists = theAddonXML.skins.entry.(src == theSRC);
  789.             if(itExists != undefined){
  790.                //We need to replace the values
  791.                itExists.name = theName;
  792.                itExists.desc = theDesc;
  793.                itExists.author = theAuthor;
  794.                itExists.src = theSRC;
  795.                itExists.date = theDateAdded;
  796.                itExists.enabled = isEnabled;
  797.                itExists.defaulton = isDefaultOn;
  798.                itExists.type = aType;
  799.                
  800.                didOverWrite = true;
  801.             }else{ //Add a new one
  802.                 theAddonXML.skins.entry +=
  803.                                         <entry>
  804.                                             <name>{theName}</name>
  805.                                             <desc>{theDesc}</desc>
  806.                                             <author>{theAuthor}</author>
  807.                                             <src>{theSRC}</src>
  808.                                             <date>{theDateAdded}</date>
  809.                                             <enabled>{isEnabled}</enabled>
  810.                                             <defaulton>{isDefaultOn}</defaulton>
  811.                                             <type>{aType}</type>
  812.                                         </entry>;
  813.             }
  814.         }else if(aService == "addon"){
  815.             var itExists = theAddonXML.addons.entry.(src== theSRC)
  816.             if(itExists != undefined){
  817.                //We need to replace the values
  818.                itExists.name = theName;
  819.                itExists.desc = theDesc;
  820.                itExists.author = theAuthor;
  821.                itExists.src = theSRC;
  822.                itExists.date = theDateAdded;
  823.                itExists.enabled = isEnabled;
  824.                itExists.defaulton = isDefaultOn;
  825.                itExists.type = aType;
  826.                
  827.                didOverWrite = true;
  828.             }else{ //Add a new one
  829.             theAddonXML.addons.entry +=
  830.                                     <entry>
  831.                                         <name>{theName}</name>
  832.                                         <desc>{theDesc}</desc>
  833.                                         <author>{theAuthor}</author>
  834.                                         <src>{theSRC}</src>
  835.                                         <date>{theDateAdded}</date>
  836.                                         <enabled>{isEnabled}</enabled>
  837.                                         <type>{aType}</type>
  838.                                     </entry>;
  839.             }
  840.         }
  841.         //Write to file (save changes)
  842.         isSaved = this.saveContentsToFile( file, theAddonXML.toString() );
  843.     }
  844.     //Report if the file was saved
  845.     if(isSaved){
  846.         return didOverWrite;
  847.     }else{
  848.         return null;
  849.     }
  850. }
  851.  
  852.  
  853. // TODO
  854. // Send this to the UTIL Library
  855. pffAddonSkin.saveContentsToFile = function(aFile, aContents){
  856.     var newWrite = PerFormancingFileIO.write(aFile, aContents, "w", "UTF-8");
  857.     if(!newWrite) {
  858.         var localeString = performancingUI.getLocaleString('cannotwritetofile', []);
  859.         alert(localeString);
  860.         return false;
  861.     }else{
  862.         return true;
  863.     }
  864.                             
  865.     return false;
  866. }
  867.  
  868. pffAddonSkin.getSkinAddonDir = function(aFileLeaf){
  869.     var file = PerFormancingDirIO.get("ProfD");
  870.     file.append("performancing");
  871.     if( aFileLeaf.match(".css") ){
  872.         file.append("skins");
  873.     }else{
  874.         file.append("addons");
  875.     }
  876.     return file;
  877. }
  878.  
  879. pffAddonSkin.getSkinAddonFile = function(aFileLeaf){
  880.     var file = pffAddonSkin.getSkinAddonDir(aFileLeaf);
  881.     file.append(aFileLeaf);
  882.     
  883.     return file;
  884. }
  885.  
  886. // nsIZipReader
  887. // A good example of it's use can be found in the Extension Manager
  888. pffAddonSkin.getZipReaderForFile = function(zipFile){
  889.    try {
  890.      var zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"]
  891.                                .createInstance(Components.interfaces.nsIZipReader);
  892.      zipReader.init(zipFile);
  893.      zipReader.open();
  894.    }
  895.    catch (e) {
  896.      zipReader.close();
  897.      return false;;
  898.    }
  899.    return zipReader;
  900. }
  901.  
  902. //For dragging and droping onto the Skin Window (not the input box)
  903. var pffCSSDragDropHandler = {
  904.     
  905.     onDragOver: function(aEvent, aFlavour, aSession){
  906.         //Foobar
  907.     },
  908.     
  909.     onDrop: function(aEvent, aTransferData, aSession){
  910.         //aTransferData.first.first.data  => gives URL
  911.         //.first.first.flavour.contentType    => text/x-moz-url
  912.         if (aTransferData.first.first.data != "") {
  913.           var theFile = aTransferData.first.first.data;
  914.           if(theFile.leafName.match(".css")){
  915.               pffAddonSkin.handleTheFile( theFile );
  916.           }else if(theFile.leafName.match(".zip") || theFile.leafName.match(".jar")){
  917.               pffAddonSkin.handleSkinZip(theFile);
  918.           }
  919.         }
  920.     },
  921.     
  922.     onDragExit: function(aEvent, aSession) {
  923.         //foobar
  924.     },
  925.     
  926.     getSupportedFlavours: function(){
  927.         var flavours = new FlavourSet();
  928.         flavours.appendFlavour("application/x-moz-file", "nsIFile");
  929.         return flavours;
  930.     },
  931.     
  932.     canHandleMultipleItems: function(){
  933.         return false;
  934.     }
  935. };
  936.  
  937. //Object inherited by Addons
  938. function pffaddonObject( aName, aVersion, aDescription, aAuthor, aId, aPffVer ) {
  939.     this.addonEnabled = false;
  940.     //this.theAddon = aAddon;
  941.     this.name = aName;
  942.     this.version = aVersion;
  943.     this.description = aDescription;
  944.     this.author = aAuthor;
  945.     this.id = aId;
  946.     this.pffver = aPffVer;
  947. }
  948.  
  949. pffaddonObject.prototype = {
  950.     
  951.     observe: function(aSubject, aTopic, aData){
  952.         var doHook = false;
  953.         try{
  954.             if (aTopic == "performancing-addon-pffstart-topic") {
  955.                     pffAddonSkin.hookInAddon( this );
  956.             }else if (aTopic == "performancing-addon-pffenable-topic") {
  957.                 if(this.id == aData){
  958.                     pffAddonSkin.enableAnAddonCallback( this, aData );
  959.                 }
  960.             }else if (aTopic == "performancing-addon-pffdisable-topic") {
  961.                 if(this.id == aData){
  962.                     pffAddonSkin.disableAnAddonCallback( this, aData );
  963.                 }
  964.             }else if (aTopic == "performancing-addon-pfftabclick-topic") {
  965.                 //alert("dude this.id: " + this.id + "\naData: " + aData + "\naTopic: " + aTopic);
  966.                 if(this.id == aData){
  967.                    this.onThisTabClick();
  968.                 }else{
  969.                 }
  970.             }
  971.         }catch(e){}
  972.     },
  973.  
  974.     hookIntoPFF:  function(doHook){
  975.        if(doHook){
  976.            gPerformancingUtil.printLog('PFF Addon Template Enabled');
  977.            this.addonEnabled = true;
  978.            this.onPFFLoad();
  979.        }else{
  980.            this.addonEnabled = false;
  981.        }
  982.     },
  983.     
  984.     init:  function(){
  985.     },
  986.     
  987.     onPFFLoad:    function(){
  988.     },
  989.     
  990.     onThisTabClick:     function(){
  991.     }
  992. }
  993.